<template>
  <el-button :disabled="!enable" round @click="showDetail">{{ button.buttonName }}</el-button>
</template>
<script setup>
import {DefaultMenuButton} from '@/components/form/DefaultFormDefinition';
import {computed, getCurrentInstance} from 'vue';
import {isEmpty, isNotEmpty, getViewId} from '@/utils/CommonUtils';

const internalInstance = getCurrentInstance();
const global = internalInstance.appContext.config.globalProperties;
const emits = defineEmits(['reload']);

defineOptions({
  name: 'DetailFormButton'
});

const props = defineProps({
  button: DefaultMenuButton,
  selections: {
    type: Array,
    default: () => [],
  },
  formDefinition: undefined
});

const enable = computed(() => {
  if (isNotEmpty(props.selections) && props.selections.length === 1) return true;
  return false;
});

function showDetail() {
  if (isEmpty(props.selections)) return;
  let formData = props.selections[0];
  toShowDetail(formData);
}

function toShowDetail(formData) {
  let viewId = getViewId(props.button);
  let url = `/plat/work/formEdit?action=detail&fullScreen=1&businessId=${formData.id}&viewId=${viewId}&processDefinitionId=${formData.process_definition_id}`;
  global.$windowManager.open(url, {
    title: props.button.buttonName,
    close: () => emits('reload')
  });
}
</script>
